<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* Basic Structures
*	These classes are little more than data structs manipulated by other objects.
*/

class saveable {
	function save() {
		dbHandler::save($this);
	}
	
	function load($id) {
		set_fields_equal($this,dbHandler::load($this,$id));
		rmv_slashes_object($this);
	}
}

/* -------- Concrete Objects --- */

class Liquid extends saveable {
	public $id; // Database and reference use
	public $name; // Name
	public $adjective; // Dark, Amber, etc.
	public $mod_drunk;
	public $mod_hunger;
	public $mod_thirst;
	
	function __construct($name, $adjective, $mod_drunk, $mod_hunger, $mod_thirst) {
		$this->name = $name;
		$this->adjective = $adjective;
		$this->mod_drunk = $mod_drunk;
		$this->mod_hunger = $mod_hunger;
		$this->mod_thirst = $mod_thirst;
	}
}

class Klass extends saveable {
	public $who_abbrev;					// 3-letters for "Who" Cmd (WIS, WAR, etc.)
	public $flag;						// How a character's class is determined in checks
	public $starting_skills = array();	// Array of starting skills;
	public $blk_mult;					// These are hidden from players but allow us to mod the person's attributes quietly
	public $int_mult;
	public $ing_mult;
	public $spd_mult;
	public $mag_mult;
	public $cxn_mult;
}

class Skill extends saveable {
	public $name;
	public $spellfunction; 	// The function it activates (scripted and stored in DB)
	public $legaltargets;
	public $min_position; 	// min to use
	public $min_mana;
	public $cooldown;		// how long to wait between this and next act
	public $dmg_msg;		// What noun it makes
	public $wearoff_msg;	// What it says when it wears off
}

// How do bulk, speed, intelligence, etc. affect a player's base stats?
// Note: Each array should be indexed by the stat level up to max level as key
// So $stat_modify->tohit[$plr->blk] = to_hit_modifier;
class stat_modify extends saveable {
	// Bulk Modified Stats
	public $tohit				= array();
	public $todam				= array();
	public $hitp				= array();
	public $shock				= array(); // (i.e. knock off feet)
	// Int Modified Stats
	public $learnspeed			= array();
	public $spellchance			= array();
	// Ingenuity Modified Stats
	public $succeedchance		= array();
	// Speed Modified Stats
	public $defense				= array();
	// Magnetism
	public $reactchance			= array();
	// Connections
	public $marketadvantages	= array();
	public $citybonus			= array();
}

/* -------- Abstract (conceptually, not programmatically) Objects --- */

// Spawners
/*class spawner extends saveable {
	public $spawner_id;
	public $item;
	public $in_room;
	
	public $timer;
	public $next_spawn_time;
	
	function __construct() {
		$this->spawner_id = uniqid("",true);
		$this->timer = DEF_NEG;
	}
	
	function spawn() {
		// The following conditionals cause an item to spawn one time (usually on boot)
		if($this->next_spawn_time===DEF_NEG) {
			return;
		} elseif($this->timer===DEF_NEG) {
			$item = clone $this->item;
			$this->in_room->items += array($item->copy_id=>$item);
			$this->next_spawn_time=DEF_NEG;
			return;
		}
		
		// TODO: Spawn By Timer
	}
}*/

// Private / Public Chatrooms
class chatroom {
	public $name;
	public $password;
	public $members = array();
	
	function add_char($char) {
		if(array_key_exists($char->copy_id,$this->members)) { return; }
		$this->send($char,"joined the room.");
		$this->members += array($char->copy_id=>$char);
	}
	
	function rmv_char($char) {
		if(!array_key_exists($char->copy_id,$this->members)) { return; }
		unset($this->members[$char->copy_id]);
		if($this->is_empty()) { 
			unset(World::$chatrooms[$this->name]);
			unset($this); 
		}
		$this->send($char,"left the room.");
	}
	
	function in_room($char) {
		return array_key_exists($char->copy_id,$this->members);
	}
	
	function is_empty() {
		return empty($this->members);
	}
	
	function send($char,$msg) {
		foreach ($this->members as $e) {
			$e->send_data(array("chatroom"=>array($this->name=>$char->name . ": " . $msg)));
		}
	}
	
	function sendto($char, $msg) {
		$char->send_data(array("chatroom"=>array($this->name=>$msg)));
	}
}

?>